<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class RaovatController extends JControllerLegacy{
	
	public function display($cachable = false, $urlparams = false)
	{
			return parent::display($cachable, $urlparams);
	}
	function create()
	{
		JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
		
		$app	= JFactory::getApplication();
		$model	= $this->getModel('Create', 'RaovatModel');
		$requestData = $this->input->post->get('jform', array(), 'array');
		
		$form	= $model->getForm();
		if (!$form)
		{
			JError::raiseError(500, $model->getError());
			return false;
		}
		$data	= $model->validate($form, $requestData);
		if ($data){
			if ($_FILES['jform']['name']['image']){
				jimport('joomla.filesystem.file');
				$filename = JFile::makeSafe($_FILES['jform']['name']['image']);
				$filename = date('Ymdhis').rand(0,10).'_'.strtolower(str_replace(" ","_", $filename));
				$src = $_FILES['jform']['tmp_name']['image'];
				$dest = JPATH_ROOT."/"."images"."/".$filename;
				if (strtolower(JFile::getExt($filename) ) == 'jpg' || 
					strtolower(JFile::getExt($filename) ) == 'png' ||
					strtolower(JFile::getExt($filename) ) == 'gif'){
				   if ( JFile::upload($src, $dest) ) {
				      $image_name = $filename;
				   } 
				} 
			}
			$db = &JFactory::getDbo();
			$query = $db->getQuery(true);
			$columns = array('title','description','category','region','image','contact','email','show_email','address','phone','published','registe_date');
			
			$param  = JComponentHelper::getParams('com_raovat');
			$published_type = $param->get('published_type');
			
 			$values = array( $db->quote($requestData['title']), 
								$db->quote($requestData['description']),
								$db->quote($requestData['category']),
								$db->quote($requestData['region']),
								$db->quote($image_name),
								$db->quote($requestData['contact']),
								$db->quote($requestData['email']),
								$db->quote($requestData['show_email']),
								$db->quote($requestData['address']),
								$db->quote($requestData['phone']),
								$db->quote($published_type),
								$db->quote(date('Y-m-d'))
			);
			 
			// Prepare the insert query.
			$query->insert($db->quoteName('#__raovat'))
			    ->columns($db->quoteName($columns))
			    ->values(implode(',', $values));
			// Reset the query using our newly populated query object.
			$db->setQuery($query);
			
			try {
			    $result = $db->query();
			    $this->setRedirect(JRoute::_('index.php?option=com_raovat&view=raovats'));
			    return ;
				
			} catch (Exception $e) {
				$this->setRedirect($_SERVER['HTTP_REFERER']);
				return ; 
			}
			
		}else {
			
			$this->setRedirect($_SERVER['HTTP_REFERER']);
			return ;
		}
		
	}
}
